/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef MC_PROC_H
#define MC_PROC_H

#include	<file.h>
#include	<inode.h>

#ifndef NSIG
#define NSIG	11
#endif
#define NPROC	 100	
#define NARGV	 12	
#define NCONTEXT 24 	

#define SFREE	0x00	
#define SRUN	0x01	
#define SIDL	0x04	
#define SREADY	0x08	
#define SSLEEP	0x10	
#define SWAIT	0x20	
#define SZOMB	0x40	

#define SLOAD	0x01	
#define SSYS	0x02
#define SLOCK	0x04

struct proc{
	int	p_exit;		
	uint	p_stat;		
	uint	p_flag;		
	uint	p_sig;		
	uint	p_esp;	
	uint 	p_lesp;
	pid_t	p_pid;	
	pid_t	p_ppid;
	pid_t	p_pgrp;
	uid_t	p_uid;
	uid_t	p_ruid;		
	gid_t	p_gid;
	gid_t	p_rgid;		
	uint	p_cpu;		
	int	p_pri;		
	int	p_nice;		
	time_t	p_time; 	
	uint	p_error; 	
	uint	*p_stack;	
	uint	*p_pgdir;	
	void	*p_wchan; 	
	void	*p_ttyp;
	int	p_signal[NSIG]; 
	void	*p_cdir;	
	void	*p_rdir; 	
	uint	p_start;	
	uint	p_end;		
	uint	p_brk;		
	uint	p_send; 	
	void	*p_ofile[NOFILE]; 
};

extern uint pagettl;
extern uint pagecnt;	
extern uint highpri;
extern struct proc *up;	
extern struct proc proc[]; 

void	swtch(void);
void	sched(void);
void	wakeup(void *chan);
void	pready(struct proc *p);
void	setpri(struct proc *p);
void	freeproc(struct proc *pp);
int	proc_init(void);
int	sleep(void *chan, int pri);
int	copy_proc_space(uint *sp, uint *dp);
int	load_exec(uint off, void *vp, uint size, struct inode *ip);
struct proc *newproc(void);
uint	load_argv(char *argv[]);

int	fork(void);
int	exit(int status);
pid_t	wait(int *status);
pid_t	getpid(void);
pid_t	getppid(void);
uid_t	getuid(void);
uid_t	geteuid(void);
gid_t	getgid(void);
gid_t	getegid(void);
int	setpgid(void);
int	execve(char *filename, char **argv, char **envp);

void	flush_tbl(void);
int	save(uint esp);
int	longjmp(uint esp);
int	save_exec(uint esp, uint eip);
int	regain(uint dst, uint src, uint esp);

void	clk_init(void);
void	clkint(void);

void	swap(struct proc *p);
int	raw_read(uint bn, uint buf);
int	raw_write(uint bn, uint buf);
int	swapin(uint addr, uint *pte);
int	swapout(uint addr, uint *pte);

#endif
